import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/view/Home'
import Dashboard from '@/view/Dashboard'


import UserMain from '@/view/sys/userMain'
import RoleMain from '@/view/sys/roleMain'
import UserChangePwd from '@/view/sys/changepwd'
import UserProfile from '@/view/sys/profile'

import EnterpriseOrderMain from '@/view/law/enterpriseOrderMain'
import EnterpriseContractMain from '@/view/law/enterpriseContractMain'

//机构管理
import EnterpriseList from '@/view/enterprise/enterprise_list'
import EnterpriseOper from '@/view/enterprise/enterprise_oper'
import EnterpriseOperStep from '@/view/enterprise/enterprise_oper_step'
import EnterpriseInvoiceMain from '@/view/enterprise/enterprise_invoiceMain'
import InvoiceMain from '@/view/enterprise/InvoiceMain'

//法律催收
import OrderList from '@/view/order/order_list'
import OrderOper from '@/view/order/order_oper'
import AccountLog from '@/view/account/account_log'


const Login = resolve => require(['@/view/Login'], resolve)

import store from '../vuex/store'

Vue.use(Router)
let user = window.localStorage.getItem('loginUserInfo')
const default_touters = [
  {
    path: '/login',
    name: '登录',
    component: Login
  },
  {
    path: '/',
    name: 'home',
    component: Home,
    redirect: '/dashboard',
    leaf: true, // 只有一个节点
    menuShow: true,
    iconCls: 'iconfont icon-home', // 图标样式class
    children: [
      {path: '/dashboard', component: Dashboard, name: '首页', menuShow: true}
    ]
  },
  {
    path: '/',
    component: Home,
    name: '设置',
    menuShow: false,
    iconCls: 'iconfont icon-setting1',
    children: [
      {path: '/sys/profile', component: UserProfile, name: '个人信息', menuShow: true},
      {path: '/sys/changepwd/:isFirstLogin', component: UserChangePwd, name: '修改密码', menuShow: true}
    ]
  }
]
const admin_routers = [
  {
    path: '/',
    component: Home,
    name: '系统管理',
    menuShow: true,
    iconCls: 'iconfont icon-users', // 图标样式class
    children: [
      {path: '/sys/userMain', component: UserMain, name: '用户管理', menuShow: true},
      {path: '/sys/roleMain', component: RoleMain, name: '角色管理', menuShow: false}
    ]
  },
  {
    path: '/',
    component: Home,
    name: '我的机构',
    menuShow: true,
    iconCls: 'iconfont icon-menufold', // 图标样式class
    children: [
      {path: '/enterprise/enterprise_list', component: EnterpriseList, name: '机构信息', menuShow: true},
      {path: '/enterprise/enterprise_oper/:id?', component: EnterpriseOper, name: '机构认证信息', menuShow: false},
      {path: '/enterprise/enterprise_oper_step/:id?', component: EnterpriseOperStep, name: '机构信息', menuShow: false},
      {path: '/enterprise/enterprise_invoiceMain/:id?', component: EnterpriseInvoiceMain, name: '机构发票', menuShow: false},
      {path: '/enterprise/invoiceMain', component: InvoiceMain, name: '发票管理', menuShow: false}
    ]
  },
    {
        path: '/',
        component: Home,
        name: '法律催收',
        menuShow: true,
        iconCls: 'iconfont icon-menufold', // 图标样式class
        children: [
            {path: '/order/order_list', component: OrderList, name: '订单列表', menuShow: true},
            {path: '/order/order_oper/:id?', component: OrderOper, name: '创建订单', menuShow: false},
            {path: '/enterprise/enterprise_oper_step/:id?', component: EnterpriseOperStep, name: '机构信息', menuShow: false},
            {path: '/enterprise/enterprise_invoiceMain/:id?', component: EnterpriseInvoiceMain, name: '机构发票', menuShow: false},
            {path: '/law/enterpriseOrderMain/:id', component: EnterpriseOrderMain, name: '订单详情', menuShow: false},
            {path: '/law/enterpriseContractMain/:id', component: EnterpriseContractMain, name: '合同详情', menuShow: false},
            {path: '/sys/roleMain', component: RoleMain, name: '订单管理', menuShow: false},
            {path: '/account/accountLog/:id', component: AccountLog, name: '帐户记录', menuShow: false}
        ]
    }
]
const customer_routers = [
  {
    path: '/',
    component: Home,
    name: '我的机构',
    menuShow: true,
    iconCls: 'iconfont icon-menufold', // 图标样式class
    children: [
      {path: '/enterprise/enterprise_list', component: EnterpriseList, name: '机构信息', menuShow: true},
      {path: '/enterprise/enterprise_oper/:id?', component: EnterpriseOper, name: '机构认证信息', menuShow: false},
      {path: '/enterprise/enterprise_oper_step/:id?', component: EnterpriseOperStep, name: '机构信息', menuShow: false},
      {path: '/enterprise/enterprise_invoiceMain/:id?', component: EnterpriseInvoiceMain, name: '机构发票', menuShow: false},
      {path: '/enterprise/invoiceMain', component: InvoiceMain, name: '发票管理', menuShow: false}
    ]
  }
]

let router = new Router({
  routes: getRouters()
// mode: 'history',
})

router.beforeEach((to, from, next) => {
  if (to.matched.length ===0) {  //如果未匹配到路由
    from.name ? next({ name:from.name }) : next('/');   //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
  }
  if (to.path.startsWith('/login')) {
    localStorage.removeItem('loginUserInfo')
    next()
  } else {
    let user = localStorage.getItem('loginUserInfo')
    if (!user) {
      next({path: '/login'})
    } else {
      let isLoadRouter = store.state.isLoadRouter
      if (!isLoadRouter) {
        let role_routers = setRoleRouters(user)
        router.addRoutes(role_routers)
        router.options.routes = router.options.routes.concat(role_routers)
        store.state.isLoadRouter = true
      }
      next()
    }
  }
})

function getRouters() {
  let user = localStorage.getItem('loginUserInfo')
  if (user) {
    store.state.isLoadRouter = true
    return default_touters.concat(setRoleRouters(user))
  } else {
    return default_touters
  }
}

function setRoleRouters(loginUserInfo) {
  loginUserInfo = JSON.parse(loginUserInfo)
  console.error(loginUserInfo)
  if (loginUserInfo.type == 0) {
    return admin_routers
  } else if (loginUserInfo.type == 1) {
    return customer_routers
  } else {
    return admin_routers
  }
}

export default router
